package com.example.androidmainshixun.view;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.adapter.NewsAdapter;
import com.example.androidmainshixun.base.BaseFragment;
import com.example.androidmainshixun.entity.NewsItem;

import java.util.ArrayList;
import java.util.List;

public class AgroSchFrag extends BaseFragment {
    private RecyclerView mnews_recycler_view;
    private NewsAdapter newsAdapter;
    private List<NewsItem> newsItemList;
    @Override
    protected int getLayoutId() {
        return R.layout.agro_school_fragment;
    }

    @Override
    protected void initView() {
    mnews_recycler_view=mView.findViewById(R.id.news_recycler_view);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mnews_recycler_view.setLayoutManager(staggeredGridLayoutManager);
        newsItemList = new ArrayList<>(getNewsData());
        newsAdapter = new NewsAdapter(newsItemList);
        mnews_recycler_view.setAdapter(newsAdapter);
    }

    @Override
    protected void initListener() {

    }
    public void setNewsList(List<NewsItem> newsList) {
        this.newsItemList = newsList;
        if (this.newsAdapter != null) {
            this.newsAdapter.notifyDataSetChanged(); // 通知数据已更改
        }
    }
    private List<NewsItem> getNewsData() {
        List<NewsItem> newsList = new ArrayList<>();
        newsList.add(new NewsItem("全国“三夏”大规模小麦机收全面展开","http://example.com/news1", R.drawable.new_image1,null, "2024-05-28"));
        newsList.add(new NewsItem("小菌菇开出“致富伞” “智慧农业设备+良种”种出新“食”尚","http://example.com/news2", R.drawable.new_image3, null,"2024-06-07"));
        newsList.add(new NewsItem("山东樱桃价格腰斩了","http://example.com/news3", R.drawable.new_image2,null, "2024-05-24"));
        return newsList;
    }
}
